<?php

class Group_WidgetController extends Zend_Controller_Action
{
	protected $_entClassIdName = NULL;
	protected $_entId = NULL;
	protected $_entIdName = NULL;
	protected $_entObj = NULL;


	public function init()
	{
		parent::init();

		$this->_entClassIdName = $this->_getParam('entity_class');
		$this->_entId = $this->_getParam('entity_id');
		if (empty($this->_entClassIdName) === FALSE || intval($this->_entId) > 0) {
			$this->_entObj = Euso_Object::getInstance($this->_entClassIdName, $this->_entId);
			if ($this->_entObj != NULL) {
				$this->_entIdName = $this->_entObj->getIdName();
			}
		}
	}


	public function memberofAction()
	{
		if ($this->_entObj === NULL && $this->_entClassIdName !== 'user') {
			throw new Zend_Controller_Action_Exception('Unexpected value', 500);
		}

		$entities = array();

		$userGroups = Group_Model_Member::memberGetNewestGroups($this->_entObj->getId());
		foreach ($userGroups as $ug) {
			$entities[] = Group_Model_Entry::getInstanceById($ug->getGroupId());
		}

		$entList = array();

		foreach ($entities as $ent) {

			$e = new stdClass();

			$e->id = $ent->getId();
			$e->idname = $ent->getIdName();
			$e->nicename = $ent->getNiceName();
			$e->url = site_url_base($ent->getUrl());

			if (Kernel::getInstance()->isModuleActive('profile')) {
				$profile = Profile_Model_Data::getInstance('group', $ent->getId());
				if ($profile != NULL) {
					$e->brief = $profile->get('tagline');
				} else {
					$e->brief = '';
				}
				$e->avatarUrl = Profile_Model_Avatar::getFileUrl('group', $ent->getId());
			} else {
				$e->brief = NULL;
				$e->avatarUrl = NULL;
			}

			$entList[] = $e;
		}

		$this->view->entityList = $entList;
		$this->view->entityCount = count($entList);
	}

	public function membersAction()
	{
		if ($this->_entObj === NULL && $this->_entClassIdName !== 'group') {
			throw new Zend_Controller_Action_Exception('Unexpected value', 500);
		}

		$entities = array();

		$groupMembers = Group_Model_Member::groupGetNewestMembers($this->_entObj->getId());
		foreach ($groupMembers as $gm) {
			$entities[] = Account_Model_Account::getInstanceById($gm->getAccountId());
		}

		$entList = array();

		foreach ($entities as $ent) {

			$e = new stdClass();

			$e->id = $ent->getId();
			$e->idname = $ent->getIdName();
			$e->nicename = $ent->getNiceName();
			$e->url = site_url_base($ent->getUrl());

			if (Kernel::getInstance()->isModuleActive('profile')) {
				$profile = Profile_Model_Data::getInstance('user', $ent->getId());
				if ($profile != NULL) {
					$e->brief = $profile->get('status');
				} else {
					$e->brief = '';
				}
				$e->avatarUrl = Profile_Model_Avatar::getFileUrl('user', $ent->getId());
			} else {
				$e->brief = NULL;
				$e->avatarUrl = NULL;
			}

			$entList[] = $e;
		}

		$this->view->entityList = $entList;
		$this->view->entityCount = count($entList);
	}
}
